package com.x.common.config;

import java.util.LinkedHashMap;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Configuration 注解描述的类为一个配置类
 * 此对象也会交给spring去管理
 * */
//1.創建springShiroConfig类
@Configuration
public class SpringShiroConfig {//spring-shiro.xml
	//2.在Shiro配置类中添加SecurityManger配置
	@Bean
	public 	SecurityManager securityManager(Realm realm) {
		DefaultWebSecurityManager sManger=
				new DefaultWebSecurityManager();
		sManger.setRealm(realm);
		return sManger;
	}
//3.在Shiro配置中添加ShiroFilterFactoryBean对象的配置，通过此对象设置资源匿名访问、认证访问
	@Bean
	public ShiroFilterFactoryBean shiroFactoryBean(SecurityManager securityManager) {
		//创建ShiroFilterFactoryBean对象
		//1.1构建对象
		ShiroFilterFactoryBean sfbean=new ShiroFilterFactoryBean();
		//1.2设置安全认证授权对象
		sfbean.setSecurityManager(securityManager);
		//1.3设置登录页面
		sfbean.setLoginUrl("/doLogin");
		//1.4定义map指定请求过滤规则(哪些资源允许匿名访问,哪些必须认证访问)
		 LinkedHashMap<String,String> map= new LinkedHashMap<>();
		 //静态资源允许匿名访问:"anon"
		map.put("/bower_components/**","anon");
		map.put("/build/**","anon"); 
		map.put("/css/**","anon");
		map.put("/dist/**","anon");
		map.put("/fonts/**","anon");
		map.put("/images/**","anon");
		map.put("/js/**","anon");
		map.put("/lib/**","anon");
		map.put("/plugins/**","anon");
		map.put("/admin/doLogin", "anon");
		map.put("/verifyCode", "anon");
		map.put("/verifyCode/checkVerify", "anon");
		//除了匿名访问的资源,其它都要认证("authc")后访问
		map.put("/**","authc");
		 sfbean.setFilterChainDefinitionMap(map);
		 return sfbean;
	}
	
}
